<?php

namespace App\Console\Commands;

use App\Http\Biz\ShopBiz;
use App\Http\Biz\SuppOrderBiz;
use App\Http\Biz\SuppReportBiz;
use App\Http\Model\PtGoods;
use App\Http\Model\PtGoodsOrderMid;
use App\Http\Model\PtGoodsSpec;
use App\Http\Model\PtPreSale;
use App\Http\Model\Shop;
use App\Http\Model\ShopAccount;
use App\Http\Model\ShopAccountRoleMid;
use App\Http\Model\ShopModule;
use App\Http\Model\ShopRoleModuleMid;
use App\Http\Model\Supp;
use App\Http\Model\SuppPurchase;
use App\Http\Model\SuppPurchaseMid;
use App\Http\Model\UserPtDict;
use App\Http\Utils\Common\Consts;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Log;

class Purchase extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'conn:purchase';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->info('start');
        $now = Carbon::now();
        $supp_ids = PtGoodsOrderMid::query()
            ->select(DB::raw('distinct(supp_id) as supp_id'))
            ->where('status', 2)
            ->where(['year' => $now->year, 'month' => $now->month, 'day' => $now->day])
            ->pluck('supp_id')->all();
        Log::info($supp_ids);
        foreach ($supp_ids as $supp_id) {
            try {
                DB::beginTransaction();
                //查询供应商售卖截止时间
                $supp = Supp::find($supp_id);
                $sale_end = $now->year . '-' . $now->month . '-' . $now->day . ' ' . $supp->sale_end . ':00';
                $time = Carbon::parse($sale_end);
                //查询供应商对应的采购商品
                $data = PtGoodsOrderMid::query()->where('supp_id', $supp_id)
                    ->where('status', 2)
                    ->where('updated_at', '<=', $time)
                    ->groupBy('spec_id')
                    ->get([DB::raw('count(order_num) as order_nums'), DB::raw('sum(price) as price'), DB::raw('sum(num) as num'), 'goods_id', 'spec_name', 'spec_id', 'goods_name']);
                if (count($data) > 0) {//该供应商今天有订单才有采购数据
                    //TODO 根据supp_id和年月日查询订单的序列号
                    $serial_numTemp = PtGoodsOrderMid::where([
                        'year' => $now->year,
                        'month' => $now->month,
                        'day' => $now->day,
                        'supp_id' => $supp_id
                    ])->where('updated_at', '<=', $time)->first();
                    if($serial_numTemp!= null){
                        //创建采购总表
                        $purchase = SuppPurchase::create([
                            'serial_num' => $serial_numTemp->serial_num,
                            'year' => $now->year,
                            'month' => $now->month,
                            'day' => $now->day,
                            'supp_id' => $supp_id
                        ]);
                        $total_price = 0;
                        $total_num = 0;
                        //创建采购字表插入商品详细规格信息
                        foreach ($data as $item) {
                            $total_price += $item->price;
                            $total_num += $item->order_nums;
//                            $spec = PtGoodsSpec::find($item->spec_id);
                            SuppPurchaseMid::create([
                                'purchase_id' => $purchase->id,
                                'goods_id' => $item->goods_id,
                                'spec_id' => $item->spec_id,
                                'spec_name' => $item->spec_name,
                                'goods_name' => $item->goods_name,
                                'sale_unit_name' => $item->sale_unit_name,
                                'num' => $item->num * $item->sale_unit_val
                            ]);
                        }
                        //统计总金额和总订单数
                        $purchase->total_money = $total_price;
                        $purchase->total_num = $total_num;
                        $purchase->save();
                    }
                }
                DB::commit();
            } catch (\Exception $e) {
                DB::rollBack();
            }
        }

        $this->info('end');
    }
}
